﻿<?php

!defined('IN_TIPASK') && exit('Access Denied');

class buyrecordmodel {

    var $db;
    var $base;
    

    function buyrecordmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    /* 获取用户扣财富值获得的问题查看的原始时间 */

    function get_buy_time($uid, $qid) {
        $buyrecord = $this->db->fetch_first("SELECT * FROM " . DB_TABLEPRE . "buyrecord WHERE qid=$qid AND uid =$uid ORDER BY time DESC");        
        $buyrecord['time'] = $buyrecord['time'];        
        return $buyrecord;
    }

    /* 获取回答表中的修改过的时间 的最大值  用来判断购买后有没有更新*/

    function get_answer_update_count($qid, $buytime) {        
        $updatecount = 0;        
          $query = $this->db->query("SELECT updatetime FROM " . DB_TABLEPRE . "answer WHERE qid=$qid");
          
          while ($answer = $this->db->fetch_array($query)) {
            //printf($buytime);exit;
            if ($answer['updatetime'] > $buytime) {
                $updatecount++;
            }
          }       
        return $updatecount;
    }

    /* 添加购买记录 */

    function add($uid, $qid) { 
        $this->db->query("INSERT INTO " . DB_TABLEPRE . "buyrecord SET qid='$qid',uid='$uid',time='{$this->base->time}'");        
        $bid = $this->db->insert_id();
    }

    /* 修改，同时重置time */

    function update_time($uid, $qid) {
        $this->db->query("UPDATE `" . DB_TABLEPRE . "buyrecord` set time='{$this->base->time}' WHERE `qid` =$qid AND `uid` =$uid");
    }

    /* 当问题被删除时，删除该问题的所有记录 */

    function remove_by_qid($qid) {
        $this->db->query("DELETE FROM `" . DB_TABLEPRE . "buyrecord` WHERE `qid`=$qid");        
    }    

}

?>
